package edu.pku.sei.modeler.explorer.registry;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.swt.graphics.Image;

import edu.pku.sei.modeler.images.ImageProvider;
import edu.pku.sei.modeler.model.core.MetaClass;
import edu.pku.sei.modeler.model.core.MetaComment;
import edu.pku.sei.modeler.model.core.MetaDataType;
import edu.pku.sei.modeler.model.core.MetaEnum;
import edu.pku.sei.modeler.model.core.MetaGeneralization;
import edu.pku.sei.modeler.model.core.MetaModel;
import edu.pku.sei.modeler.model.core.MetaNamedElement;
import edu.pku.sei.modeler.model.core.MetaPackage;
import edu.pku.sei.modeler.model.core.MetaReference;
import edu.pku.sei.modeler.model.structure.MetaDiagram;
import edu.pku.sei.modeler.project.AbstractModelingProject;

public final class CommonLabelProvider implements ITreeViewLabelProvider {
	public static CommonLabelProvider DEFAULT_INSTANCE = new CommonLabelProvider();
	public String getText(Object element) {
		if (element instanceof IProject) {
			return ((IProject)element).getName();
		}
		else if(element instanceof IFolder) {
			return ((IFolder)element).getName();
		}
		else if(element instanceof IFile) {
			return ((IFile)element).getName();
		}
		else if (element instanceof MetaNamedElement) {
			return ( (MetaNamedElement)element ).getName();
		}
		else if(element instanceof MetaComment) {
			return new String("comment");
		}
		else if(element instanceof MetaGeneralization) {
			MetaGeneralization gen = (MetaGeneralization)element;
			if(gen.getGeneral()!=null) {
				return "base:"+gen.getGeneral().getName();
			}
			return "no base";
		}
		else if(element instanceof AbstractModelingProject) {
			AbstractModelingProject proj = (AbstractModelingProject)element;
			return proj.getProject().getName();
		}
		
		return new String("default");
	}
	
	public Image getImage(Object element) {
		ImageDescriptor iDesc;
		if(element instanceof IProject) {
			IProject proj = (IProject)element;
			if(proj.isOpen()) {
				iDesc = ImageProvider.getImageDescriptor(ImageProvider.PROJECT_IMG);
			}
			else {
				iDesc = ImageProvider.getImageDescriptor(ImageProvider.PROJECT_CLOSED_IMG);
			}
		}
		else if(element instanceof IFolder) {
			iDesc = ImageProvider.getImageDescriptor(ImageProvider.FOLDER_IMG);
		}
		else if(element instanceof IFile) {
			iDesc = ImageProvider.getImageDescriptor(ImageProvider.FILE_IMG);
		}
		else if (element instanceof AbstractModelingProject) {
			iDesc = ImageProvider.getImageDescriptor(ImageProvider.PROJECT);
		} else if (element instanceof MetaEnum) {
			iDesc = ImageProvider.getImageDescriptor(ImageProvider.ENUM);
		} else if (element instanceof MetaDataType) {
			iDesc = ImageProvider.getImageDescriptor(ImageProvider.ATTRIBUTE);
		} else if (element instanceof MetaModel) {
			iDesc = ImageProvider.getImageDescriptor(ImageProvider.MODEL_VIEW);
		} else if (element instanceof MetaPackage) {
			iDesc = ImageProvider.getImageDescriptor(ImageProvider.PACKAGE);
		} else if (element instanceof MetaClass) {
			iDesc = ImageProvider.getImageDescriptor(ImageProvider.CLASS_ENTRY);
		} else if (element instanceof MetaComment) {
			iDesc = ImageProvider.getImageDescriptor(ImageProvider.COMMENT);
		} else if (element instanceof MetaDiagram) {
			iDesc = ImageProvider.getImageDescriptor(ImageProvider.DIAGRAM);
		} else if (element instanceof MetaReference) {
			iDesc = ImageProvider.getImageDescriptor(ImageProvider.ASSOCIATION_ENTRY);
		} else if (element instanceof MetaGeneralization) {
			iDesc = ImageProvider.getImageDescriptor(ImageProvider.ASSOCIATION_ENTRY);
		} else if (element instanceof MetaNamedElement) {
			iDesc = ImageProvider.getImageDescriptor(ImageProvider.COMMENT_ENTRY);
		} 
		else {
			iDesc = ImageProvider.getImageDescriptor(ImageProvider.UNKNOWN);
		}	
		return iDesc.createImage();
	}

	public void addListener(ILabelProviderListener listener) {
		// TODO Auto-generated method stub

	}

	public void dispose() {
		// TODO Auto-generated method stub

	}

	public boolean isLabelProperty(Object element, String property) {
		// TODO Auto-generated method stub
		return false;
	}

	public void removeListener(ILabelProviderListener listener) {
		// TODO Auto-generated method stub

	}

	@Override
	public String getNature() {
		return AbstractModelingProject.COMMON_NATURE;
	}

}
